-
Notifications
You must be signed in to change notification settings - Fork 361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: [M3-7667] - (Proof of Concept) Add tagging capability for Cypress tests #10475
test: [M3-7667] - (Proof of Concept) Add tagging capability for Cypress tests #10475
Conversation
Coverage Report: ✅ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yarn cy:run works, Also test for create-linode.spec.ts passed
✔ All specs passed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall this looks good to me 👍
I believe I read that cy.state
is deprecated and will be removed in future releases (albeit it seems to have stuck around for several major releases). https://docs.cypress.io/guides/references/changelog#10-5-0
cy.state('subject') is deprecated and reading from it will log a warning to the console. Prefer cy.currentSubject() instead. Addresses [#23092](https://github.com/cypress-io/cypress/issues/23092).
Unless I'm misunderstanding something?
Thanks @jaalah-akamai
Not quite -- |
Merging despite test failure because the failing test is already present in |
Description 📝
Proof of concept to give us the capability to apply arbitrary tags to our Cypress tests and run Cypress against subsets of tags.
Motivation
The main goal of this effort is to give us more flexibility in how/when we run our tests. M3-7667 and M3-8072 explore some of the challenges we're facing as our test suite grows and as more teams rely on Cloud and its tests for their own purposes.
Usage
Tagging Tests
When developing tests, we can tag them using
cy.tag
orcy.addTag
. Tags can be applied to individual tests, or to multiple tests usingbeforeEach
hooks.cy.tag
will replace any tags that have already been set, whilecy.addTag
will add tags in addition to any that may have been set via hooks.A quick example of what this looks like:
Running Tagged Tests
By default, running
yarn cy:run
will run all tests regardless of how they're tagged. To specify which tests should run using tags, users can use theCY_TEST_TAGS
environment variable:Tags can be composed and negated:
Implementation
The tag utils work by modifying each test's Mocha context when Cypress executes the test, which happens before the test runs. Because
cy.tag
andcy.addTag
are not part of Cypress's command queue, they can be called at any point in a test and will still apply.Why Roll Our Own?
I opted to implement our own tagging mechanism rather than rely on an existing plugin or package. There are two packages that are relevant, and inspiration was drawn from each, but neither is perfectly suitable for our needs:
it(...)
,describe(...)
, etc. functions exposed by Mocha. I did take a lot of inspiration from its tag querying/composing mechanism, however.Downsides, Caveats, etc.
Performance
Because skipped tests are identified at runtime, Cypress must still preprocess all spec files included in its run, even if all the tests within the spec ultimately get skipped. Consequently, running the test suite with all tests skipped still takes approximately 10 minutes (when running locally on my machine) to account for the preprocessing.
Changes to
cy.defer()
This implementation required changes to
cy.defer()
so that it consumes a function that returns a Promise rather than consuming a Promise directly. This is necessary because tests that get skipped still get executed by Cypress (without being run), so Promises that are initialized outside of Cypress's command queue get rejected when the test is skipped, resulting in errors and exceptions in certain cases (e.g. Axios requests getting aborted).Reliance on
cy.state()
This implementation requires use of the
cy.state()
function in order to access the tests' Mocha contexts, which is undocumented and could be changed. We're already relying oncy.state()
(albeit to a much lesser extent) because it exposes the only method available to skip a Cypress test at runtime. Even if this implementation were to change, we can adapt as long as Cypress continues to expose the Mocha context.Changes 🔄
List any change relevant to the reviewer.
tag
andaddTag
utils, as well as global aliasescy.tag
andcy.addTag
CY_TEST_TAGS
environment variable to allow users to specify which tests to run using tagscreate-linode.spec.ts
Preview 📷
Running Linode create tests that are tagged with the e2e tag:
Tag info that gets logged when Cypress starts, intended to help troubleshoot tag queries:
Error message when the user specifies an invalid tag query:
How to test 🧪
yarn cy:run
runs all of the tests (you can stop after a few tests have run, no need to wait for the entire suite -- CI will help us double check this, too)CY_TEST_TAGS='method:e2e' yarn cy:run -s "cypress/e2e/core/linodes/create-linode.spec.ts"
runs only the tests increate-linode.spec.ts
which are tagged withmethod:e2e
As an Author I have considered 🤔
Check all that apply